package ruleinterceptor

import (
	"context"
	"encoding/json"
	"fmt"
	"github.com/go-playground/validator"
	"strconv"
	"time"
	"vpn-management-server/logger"
	"vpn-management-server/network"
	"vpn-management-server/security/code"
	"vpn-management-server/src/model/adminlogmodel"
	"vpn-management-server/src/model/adminmodel"
	"vpn-management-server/src/model/rulemodel"
	"vpn-management-server/src/model/usermodel"
	"vpn-management-server/src/service/adminlogservice"
	"vpn-management-server/src/service/adminservice"
)

// RuleValidator 添加规则验证器
func RuleValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var rule rulemodel.Rule
	err := json.Unmarshal(request.Parameters, &rule)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to rule error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(rule)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 验证管理员权限 只有超级管理员和规则管理员才可以 添加规则
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&rule)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// GetAllRuleValidator 分页获取规则验证器
func GetAllRuleValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var AllRuleGet rulemodel.GetAllRule
	err := json.Unmarshal(request.Parameters, &AllRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to ruleGet error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(AllRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 规则管理员和超级管理员可以分页查看所有规则
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&AllRuleGet)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// DeleteRuleValidator 删除规则验证器
func DeleteRuleValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var ruleIDList rulemodel.RuleIDList
	err := json.Unmarshal(request.Parameters, &ruleIDList)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to rule id list error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(ruleIDList)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 用户组管理员不能够删除规则
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&ruleIDList)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// GrantRGroupValidator  规则分配器
func GrantRGroupValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var grantRGroup rulemodel.GetUserRule
	err := json.Unmarshal(request.Parameters, &grantRGroup)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to grantRGroup error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(grantRGroup)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 超级管理员和系统管理员可以分配
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&grantRGroup)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// GetUGroupRuleValidator 分页获取用户组规则
func GetUGroupRuleValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroupRuleGet rulemodel.GetUGroupRule
	err := json.Unmarshal(request.Parameters, &UGroupRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GetUGroupRule error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(UGroupRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 规则管理员和超级管理员可以分页查看所有规则
	// 到底是哪个管理员可以查看?
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&UGroupRuleGet)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// DeleteUGroupRuleValidator 删除用户组规则
func DeleteUGroupRuleValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroupRuleIDList rulemodel.UGroupRuleIDList
	err := json.Unmarshal(request.Parameters, &UGroupRuleIDList)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to rule id list error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(UGroupRuleIDList)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 只有用户组管理员能够删除用户组内规则
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&UGroupRuleIDList)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// RecoverUGroupRuleValidator 恢复用户组规则
func RecoverUGroupRuleValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroupRuleIDList rulemodel.UGroupRuleIDList
	err := json.Unmarshal(request.Parameters, &UGroupRuleIDList)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to rule id list error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(UGroupRuleIDList)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 只有用户组管理员能够删除用户组内规则
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.RuleAdmin || rank == adminmodel.SuperAdmin {
		request.SetModel(&UGroupRuleIDList)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// GrantRuleToUGroupValidator  分配权限到用户组验证器
func GrantRuleToUGroupValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var grant rulemodel.GrantUGroup
	err := json.Unmarshal(request.Parameters, &grant)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GrantUGroup error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(grant)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 超级管理员和规则管理员只能分配规则和规则组给用户组
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&grant)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// GrantRuleToUserValidator  分配权限到用户验证器
func GrantRuleToUserValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var grant rulemodel.GrantUser
	err := json.Unmarshal(request.Parameters, &grant)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GrantUser error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(grant)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 用户组管理员只能分配规则和规则组给用户
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&grant)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetRuleByRuleIDValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var RuleID rulemodel.RuleID
	err := json.Unmarshal(request.Parameters, &RuleID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to RuleInfo error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(RuleID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	//三种管理员都可以获取规则具体信息
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin || rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&RuleID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetUGroupAllRuleInfosValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var UGroupID rulemodel.UGroupID
	err := json.Unmarshal(request.Parameters, &UGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to UGroupID error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(UGroupID)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	//三种管理员都可以获取规则具体信息
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin || rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&UGroupID)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

// DepriveRuleFromUGroupValidator 剥夺分配给用户组的规则
func DepriveRuleFromUGroupValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var deprive rulemodel.DepriveUGroup
	err := json.Unmarshal(request.Parameters, &deprive)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to DepriveUGroup error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(deprive)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 超级管理员和规则管理员只能剥夺用户组的规则或规则组
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin {
		request.SetModel(&deprive)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func DepriveRuleFromUserValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var deprive rulemodel.DepriveUser
	err := json.Unmarshal(request.Parameters, &deprive)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to DepriveUser error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(deprive)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	// TODO 用户组管理员只能剥夺用户的规则或规则组
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.UserGroupAdmin {
		request.SetModel(&deprive)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetUserRuleByPageValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var userRuleGet rulemodel.GetUserRule
	err := json.Unmarshal(request.Parameters, &userRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GetUserRule error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(userRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&userRuleGet)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func GetUserRuleValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var userName usermodel.UserName
	err := json.Unmarshal(request.Parameters, &userName)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to GetUserRule error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(userName)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	request.SetModel(&userName)
	return true
}

func GetRGroupRulesByPageValidator(ctx context.Context, request *network.NetRequest, response *network.NetResponse) bool {
	var rGroupRuleGet rulemodel.RGroupRuleGet
	err := json.Unmarshal(request.Parameters, &rGroupRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "UnMarshal Parameters to RGroupRuleGet error,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "parse parameters fail"
		return false
	}
	valid := validator.New()
	err = valid.Struct(rGroupRuleGet)
	if err != nil {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "validate Parameters fail,", err)
		response.Code = code.BAD_REQUEST
		response.ErrorMsg = "validate parameters fail"
		return false
	}
	if rank := adminservice.GetAdminRankService(ctx, request, response); rank == adminmodel.SuperAdmin || rank == adminmodel.RuleAdmin || rank == adminmodel.UserGroupAdmin {
		request.SetModel(&rGroupRuleGet)
		return true
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "have no authority")
		response.Code = code.UNAUTHORIZED
		response.ErrorMsg = "have no authority"
		return false
	}
}

func AddRuleLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	rule, ok := request.GetModel().(*rulemodel.Rule)
	if ok {
		adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
			Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
			Source:    request.GetRemote(),
			Location:  "地址",
			Behavior:  fmt.Sprintf("添加规则%s", rule.Name),
			TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
		})
	} else {
		logger.Logger.ErrorWithConn(ctx.Value("conn_id").(string), "Transform interface to rule fail")
	}
}

func DeleteRulesLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	ruleIDList := request.GetModel().(*rulemodel.RuleIDList)
	// TODO 记录删除规则的名字，而不是ID
	adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
		Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
		Source:    request.GetRemote(),
		Location:  "地址",
		Behavior:  fmt.Sprintf("删除规则%v", ruleIDList.IDs),
		TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
	})
}

func RecoverRulesLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	UGroupRuleIDList := request.GetModel().(*rulemodel.UGroupRuleIDList)
	adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
		Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
		Source:    request.GetRemote(),
		Location:  "地址",
		Behavior:  fmt.Sprintf("恢复规则%v", UGroupRuleIDList.IDs),
		TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
	})
}

func GrantRuleToUGroupLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	grantUGroup := request.GetModel().(*rulemodel.GrantUGroup)
	adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
		Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
		Source:    request.GetRemote(),
		Location:  "地址",
		Behavior:  fmt.Sprintf("管理员%s授予规则给用户组%d", grantUGroup.Signer, grantUGroup.UGroupID),
		TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
	})
}

func DepriveRuleFromUGroupLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	deprive := request.GetModel().(*rulemodel.DepriveUGroup)
	adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
		Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
		Source:    request.GetRemote(),
		Location:  "地址",
		Behavior:  fmt.Sprintf("取消用户组%d的规则", deprive.UGroupID),
		TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
	})
}

func DeleteUGroupRulesLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	uGroupRuleIDList := request.GetModel().(*rulemodel.UGroupRuleIDList)
	adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
		Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
		Source:    request.GetRemote(),
		Location:  "地址",
		Behavior:  fmt.Sprintf("删除用户组%d的规则", uGroupRuleIDList.UGroupID),
		TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
	})
}

func GrantRuleToUserLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	grantUser := request.GetModel().(*rulemodel.GrantUser)
	adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
		Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
		Source:    request.GetRemote(),
		Location:  "地址",
		Behavior:  fmt.Sprintf("管理员%s授予规则给用户%d", grantUser.Signer, grantUser.UserID),
		TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
	})
}

func DepriveRuleFromUserLogToChan(ctx context.Context, request *network.NetRequest, response *network.NetResponse) {
	depriveUser := request.GetModel().(*rulemodel.DepriveUser)
	adminlogservice.AddAdminLogToChan(adminlogservice.ChanAdminLogs, adminlogmodel.AdminLog{
		Name:      adminservice.GetCurrAdminService(ctx, request, response).Name,
		Source:    request.GetRemote(),
		Location:  "地址",
		Behavior:  fmt.Sprintf("管理员取消用户%d的规则", depriveUser.UserID),
		TimeStamp: strconv.FormatInt(time.Now().Unix(), 10),
	})
}
